package com.tools.controller;

import animatefx.animation.FadeInUp;
import com.tools.constants.BaseContext;
import com.tools.constants.Constant;
import com.tools.utils.SqlLiteUtil;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * IndexController
 *
 * @author < a href=" ">ZhangYang</ a>
 * @version v1.0.01
 * @date 2018-07-25 16:33
 */
public class MenuController extends BaseController {
  private static final Logger LOG = Logger.getLogger(MenuController.class);

  @FXML
  private Pane codeGenerator;
  @FXML
  private Pane counter;
  @FXML
  private Pane imgZip;

  @Override
  public void initialize(URL location, ResourceBundle resources) {
    try {
      System.out.println(SqlLiteUtil.getSetting());
    } catch (Exception e) {
      e.printStackTrace();
    }

    /** 打开代码生成窗口 **/
    codeGenerator.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
      try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Thread.currentThread().getContextClassLoader().getResource(FXMLPage.DATABASE.getFxml()));
        AnchorPane dataBaseOverview = loader.load();
        this.getPrimaryStage().setTitle(Constant.STRING_TITLE_CODE);
        this.getIndex().setCenter(dataBaseOverview);
        DataBaseController dataBaseController = loader.getController();

        BaseContext.controllerMap.put("dataBaseController", dataBaseController);
        new FadeInUp(dataBaseOverview).play();
      } catch (IOException e) {
        e.printStackTrace();
      }
    });

    /** 打开计算器窗口 **/
    counter.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
      try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Thread.currentThread().getContextClassLoader().getResource(FXMLPage.COUNTER.getFxml()));
        AnchorPane counterOverview = loader.load();
        this.getPrimaryStage().setTitle(Constant.STRING_TITLE_COUNTER);
        this.getIndex().setCenter(counterOverview);
        this.setIndex(getIndex());

        BaseContext.controllerMap.put("counterController", loader.getController());
        new FadeInUp(counterOverview).play();
      } catch (IOException e) {
        e.printStackTrace();
      }
    });

    /** 打开图片压缩窗口 **/
    imgZip.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
      try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Thread.currentThread().getContextClassLoader().getResource(FXMLPage.IMGZIPPAGE.getFxml()));
        AnchorPane imgZipOverview = loader.load();
        this.getPrimaryStage().setTitle(Constant.STRING_TITLE_IMGZIP);
        this.getIndex().setCenter(imgZipOverview);

        BaseContext.controllerMap.put("counterController", loader.getController());
        new FadeInUp(imgZipOverview).play();
      } catch (IOException e) {
        e.printStackTrace();
      }
    });
  }

}
